<?php
/**
 * QQ第三方登录驱动
 */
namespace mcOAuth\Driver;
class qq extends \mcOAuth\OAuth{
    protected $API_URL = 'https://graph.qq.com/';

    /**
     * 生成获取AuthorizeCode地址
     * @return url
     */
    protected function buildAuthorizeCodeURL(){
        $params=array(
            'response_type' =>'code',
            'client_id'     =>$this->config['APP_ID'],
            'redirect_uri'  =>$this->config['callback'],
            'scope'         =>$this->config['scope'],
            'display'       =>$this->config['display'],
            'state'         =>$this->AuthorizeState,
        );
        $url=$this->buildAPI('oauth2.0/authorize',$params);
        return $url;
    }

    /**
     * 获取AccessToken
     */
    protected function doGetAccessToken(){
        $params = array(
            'grant_type'    =>'authorization_code',
            'client_id'     =>$this->config['APP_ID'],
            'client_secret' =>$this->config['APP_KEY'],
            'code'          =>$this->AuthorizeCode,
            'redirect_uri'  =>$this->config['callback'],
        );
        $result=$this->requestAPI('oauth2.0/token',$params);
        if(!empty($result)){
            parse_str($result,$data);
            return $this->setAccessTokenAttr($data);
        }else{
            exception($result);
        }
    }

    /**
     * 格式化获取的AccessToken数据
     */
    protected function setAccessTokenAttr($data){
        $this->AccessToken=$data['access_token'];
        $params = array(
            'access_token'  => $data['access_token'],
            'expires_in'    => $data['expires_in'],
            'refresh_token' => $data['refresh_token'],
            'openid'        => $this->doGetOpenID(),
            'create_at'     => time()
        );
        return $params;
    }

    /**
     * 获取OpenID
     */
    protected function doGetOpenID(){
        $params = array(
            'access_token'  =>$this->AccessToken
        );
        $result=$this->requestAPI('oauth2.0/me',$params);
        if(!empty($result)){
            preg_match('/\{.*?\}/',$result,$json);
            $data=json_decode($json[0],true);
            if(isset($data['error'])){
                exception($data['error_description']);
            }
            return $this->setOpenIDAttr($data);
        }else{
            exception($result);
        }
    }

    /**
     * 格式化获取的OpenID数据
     */
    protected function setOpenIDAttr($data){
        $this->OpenID=$data['openid'];
        return $data['openid'];
    }

    /**
     * 获取用户信息
     */
    protected function doGetUserInfo(){
        $params=array(
            'access_token'       =>$this->AccessToken,
            'oauth_consumer_key' =>$this->config['APP_ID'],
            'openid'             =>$this->OpenID
        );
        $result=$this->requestAPI('user/get_user_info',$params);
        if(!empty($result)){
            $data=json_decode($result,true);
            $data=$this->setUserInfoAttr($data);
            return $data;
        }else{
            exception('获取UserInfo失败，请重试！');
        }
    }

    /**
     * 格式化获取的用户信息数据
     */
    protected function setUserInfoAttr($data){
        $params = array(
            'openid'  => $this->OpenID,
            'channel' => $this->config['channel'],
            'nickname'=> $data['nickname'],
            'gender'  => $data['gender'] == "男" ? 'm' : 'f',
            'avatar'  => $data['figureurl_qq_2'] ? $data['figureurl_qq_2'] : $data['figureurl_qq_1']
        );
        return $params;
    }
}
